﻿#region Using directives

using System;
using System.Collections.Generic;
using System.Text;
using System.Xml;
using MCMLXVII.BackEnd.Services.OrganizationChart.Messages;
using MCMLXVII.BackEnd.Data.OrganizationChart;
using MCMLXVII.BackEnd.Services.Common.Messages;
using MCMLXVII.BackEnd.Pipeline;
using MCMLXVII.BackEnd.Data.SystemDatabase;
using MCMLXVII.Core.Common.BackEnd;
using MCMLXVII.BackEnd.Data.OrganizationChart.DataSets;
using MCMLXVII.Core.Common;

#endregion

namespace MCMLXVII.BackEnd.Services.OrganizationChart.BusinessActions
{
    public static class OrganizationLists
    {

        public static msgMembershipInfo GetOrganizationTreeMemberDesc(msgGenericItemRequest<int> req)
        {
            DSOrganizationTreeMemberInfo.MemberOfRow  dr = TreeLists.GetOrganizationTreeMemberDesc(req.Item);
            if (dr == null)
                return null;
            else
            {
                return new msgMembershipInfo(dr.MemberID, dr.MemberClass, dr.Description);
            }
        }
        public static msgMembershipInfo[] GetOrganizationTreeRoleMembers(msgFindMembersRequest req)
        {
            DSOrganizationTreeMemberInfo.MemberOfDataTable dt = TreeLists.GetOrganizationTreeRoleMembers(req.ClassID, req.FindPattern);
            if (dt == null)
                return null;
            else
            {
                msgMembershipInfo[] mem = new msgMembershipInfo[dt.Rows.Count];
                int i = 0;
                foreach (DSOrganizationTreeMemberInfo.MemberOfRow row in dt.Rows)
                {
                    mem[i] = new msgMembershipInfo(row.MemberID, row.MemberClass, row.Description);
                    i++;
                }
                return mem;
            }
        }
        
        
        /// <summary>Implements Action GetOrganizationTreeMembersClasses</summary>
        public static msgOrganizationTreeMembersClass[] GetOrganizationTreeMembersClasses(msgVoidRequest req)
        {
            // Load Members Classes
            int MemberID = Security.GetMemberIDFromSession();
            DSOrganizationMembersClasses.OrganizationMembersClassesDataTable dt = TreeLists.GetOrganizationMembersClasses(MemberID);
            msgOrganizationTreeMembersClass[] response = new msgOrganizationTreeMembersClass[dt.Rows.Count];
            int i = 0;
            foreach (DSOrganizationMembersClasses.OrganizationMembersClassesRow row in dt.Rows)
            {
                response[i] = new msgOrganizationTreeMembersClass(row.OrganizationMemberClassID, row.Description, (row.IdentifiesFinalUser == "Y"), (row.IsGroup == "Y"), (row.IsSecuritySelectable == "Y"), (row.IsWorkSelectable == "Y"), (row.IsOrganizationTreeMember == "Y"), (row.IsRole == "Y"), row.AssemblyAndClass, row.BasicProperties);
                i++;
            }
            // Load Members - Members Relation
            DSMembersClassMembers.OrganizationMembersClassesMembersDataTable dtM = TreeMembers.GetOrganizationMembersClassesMembers(MemberID);
            int ActMember = -1;
            int ActmemberIdx = -1;
            foreach (DSMembersClassMembers.OrganizationMembersClassesMembersRow rowM in dtM.Rows)
            {
                if (ActMember != rowM.OrganizationMemberClassID)
                {
                    ActMember = rowM.OrganizationMemberClassID;
                    ActmemberIdx = -1;
                    for (i = 0; i < response.Length; i++)
                    {
                        if (response[i].OrganizationMemberClassID == ActMember)
                        {
                            ActmemberIdx = i;
                            break;
                        }

                    }
                }
                if (ActmemberIdx != -1)
                {
                    response[ActmemberIdx].AddMember(rowM.OrganizationMemberMembersClassID);
                }
            }
            return response;
        }

        //Deprecated
        public static msgMembershipInfo[] GetOrganizationTreeGroups(msgFindMembersRequest req)
        {                            
            DSOrganizationTreeMemberInfo.MemberOfDataTable  dt = TreeLists.GetOrganizationTreeGroups(req.FindPattern);
            if (dt == null)
                return null;
            else
            {
                msgMembershipInfo[] mem = new msgMembershipInfo[dt.Rows.Count];
                int i = 0;
                foreach (DSOrganizationTreeMemberInfo.MemberOfRow row in dt.Rows)
                {
                    mem[i] = new msgMembershipInfo(row.MemberID, row.MemberClass, row.Description);
                    i++;
                }
                return mem;
            }
        }

        public static msgMembershipInfo[] GetOrganizationTreeMembers(msgFindMembersRequest req)
        {
            DSOrganizationTreeMemberInfo.MemberOfDataTable dt = TreeLists.GetOrganizationTreeMembers(req.ClassID, req.FindPattern);
            if (dt == null)
                return null;
            else
            {
                msgMembershipInfo[] mem = new msgMembershipInfo[dt.Rows.Count];
                int i = 0;
                foreach (DSOrganizationTreeMemberInfo.MemberOfRow row in dt.Rows)
                {
                    mem[i] = new msgMembershipInfo(row.MemberID, row.MemberClass, row.Description);
                    i++;
                }
                return mem;
            }
        }

    }
}
